package com.foodorder.repository;

import com.foodorder.entity.Order;
import com.foodorder.entity.Order.OrderStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 订单数据访问层
 */
@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
    
    /**
     * 根据用户ID查询订单，按创建时间降序排序
     */
    Page<Order> findByUserIdOrderByCreatedAtDesc(Long userId, Pageable pageable);
    
    /**
     * 根据用户ID和订单状态查询订单，按创建时间降序排序
     */
    Page<Order> findByUserIdAndStatusOrderByCreatedAtDesc(Long userId, OrderStatus status, Pageable pageable);
    
    /**
     * 根据店铺ID查询订单，按创建时间降序排序
     */
    Page<Order> findByStoreIdOrderByCreatedAtDesc(Long storeId, Pageable pageable);
    
    /**
     * 根据店铺ID查询订单列表
     */
    List<Order> findByStoreId(Long storeId);
    
    /**
     * 根据店铺ID和订单状态查询订单，按创建时间降序排序
     */
    Page<Order> findByStoreIdAndStatusOrderByCreatedAtDesc(Long storeId, OrderStatus status, Pageable pageable);
    
    /**
     * 根据订单号查询订单
     */
    Optional<Order> findByOrderNo(String orderNo);
    
    /**
     * 统计用户的订单数量
     */
    long countByUserId(Long userId);
    
    /**
     * 统计用户特定状态的订单数量
     */
    long countByUserIdAndStatus(Long userId, OrderStatus status);
    
    /**
     * 统计店铺的订单数量
     */
    long countByStoreId(Long storeId);
    
    /**
     * 统计店铺特定状态的订单数量
     */
    long countByStoreIdAndStatus(Long storeId, OrderStatus status);
    
    /**
     * 根据订单状态统计数量
     */
    long countByStatus(OrderStatus status);
    
    /**
     * 根据订单类型统计数量
     */
    long countByType(Order.OrderType type);
    
    /**
     * 根据创建时间查询订单
     */
    List<Order> findByCreatedAtAfter(LocalDateTime dateTime);
    
    /**
     * 根据创建时间范围查询订单
     */
    List<Order> findByCreatedAtBetween(LocalDateTime startTime, LocalDateTime endTime);
    
    /**
     * 根据创建时间查询订单数量
     */
    long countByCreatedAtAfter(LocalDateTime dateTime);
    
    /**
     * 根据创建时间范围查询订单数量
     */
    long countByCreatedAtBetween(LocalDateTime startTime, LocalDateTime endTime);
    
    /**
     * 查询待支付且超过指定时间的订单
     */
    @Query("SELECT o FROM Order o WHERE o.status = com.foodorder.entity.Order$OrderStatus.PENDING_PAYMENT AND o.createdAt < :timeThreshold")
    List<Order> findPendingPaymentOrdersBeforeTime(@Param("timeThreshold") LocalDateTime timeThreshold);
    
    /**
     * 统计店铺在特定时间范围内的订单数量
     */
    long countByStoreIdAndCreatedAtBetween(Long storeId, LocalDateTime startTime, LocalDateTime endTime);
    
    /**
     * 计算用户已完成订单的总金额
     */
    @Query("SELECT SUM(o.totalAmount) FROM Order o WHERE o.userId = :userId AND o.status = :status")
    BigDecimal sumTotalAmountByUserIdAndStatus(@Param("userId") Long userId, @Param("status") OrderStatus status);
    
    /**
     * 计算店铺已完成订单的总金额
     */
    @Query("SELECT SUM(o.totalAmount) FROM Order o WHERE o.storeId = :storeId AND o.status = :status")
    BigDecimal sumTotalAmountByStoreIdAndStatus(@Param("storeId") Long storeId, @Param("status") OrderStatus status);
    
    /**
     * 计算店铺在特定时间范围内已完成订单的总金额
     */
    @Query("SELECT SUM(o.totalAmount) FROM Order o WHERE o.storeId = :storeId AND o.status = :status AND o.createdAt BETWEEN :startTime AND :endTime")
    BigDecimal sumTotalAmountByStoreIdAndStatusAndCreatedAtBetween(
            @Param("storeId") Long storeId, 
            @Param("status") OrderStatus status, 
            @Param("startTime") LocalDateTime startTime, 
            @Param("endTime") LocalDateTime endTime);
    
    /**
     * 查找需要处理的超时未支付订单
     */
    @Query("SELECT o FROM Order o WHERE o.status = :status AND o.createdAt < :timeThreshold")
    List<Order> findTimeoutOrders(@Param("status") OrderStatus status, @Param("timeThreshold") LocalDateTime timeThreshold);
    
    /**
     * 查找用户最近的一个订单
     */
    Order findFirstByUserIdOrderByCreatedAtDesc(Long userId);
    
    /**
     * 查找店铺最近的一个订单
     */
    Order findFirstByStoreIdOrderByCreatedAtDesc(Long storeId);
    
    /**
     * 按支付方式统计订单数量
     */
    @Query("SELECT o.paymentMethod, COUNT(o) FROM Order o WHERE o.storeId = :storeId AND o.status = :status GROUP BY o.paymentMethod")
    List<Object[]> countOrdersByPaymentMethod(@Param("storeId") Long storeId, @Param("status") OrderStatus status);
    
    /**
     * 按时间段统计订单数量
     */
    @Query("SELECT o FROM Order o WHERE o.storeId = :storeId AND o.createdAt >= :startTime AND o.createdAt < :endTime")
    List<Order> findOrdersByStoreIdAndTimeRange(@Param("storeId") Long storeId, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
}